<template>
  <div class="login-container">
    <video
      src="@/assets/video.mp4"
      autoplay="autoplay"
      loop="loop"
      muted="muted"
      style="width: 100%; height: 100%; object-fit: fill"
    />
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="rules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <img src="@/assets/login.png" alt="">
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="请输入手机号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          name="password"
          tabindex="2"
          auto-complete="on"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        />
        <span class="svg-eye" @click="eyeClick">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.code"
          name="code"
          placeholder="请输入验证码"
          type="text"
          tabindex="3"
          auto-complete="on"
        />
        <img
          ref="img"
          width="80%"
          style="margin-left:10%"
          :src="imageUrl"
          alt=""
          class="code-image"
          @click="getverify"
        >
      </el-form-item>

      <!-- <el-form-item> -->
      <el-button
        class="button"
        type="primary"
        @click="hLongin"
      >登录</el-button>
      <!-- </el-form-item> -->
    </el-form>
  </div>
</template>

<script>
import { verify } from '@/api/user'
import { randomNum } from '@/utils/index'
export default {
  name: 'Login',
  data() {
    return {
      imageUrl: '',
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        key: ''
      },
      rules: {
        account: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
          // {
          //   pattern: /^1[3-9]\d{9}$/,
          //   message: '请输入正确手机号',
          //   trigger: 'blur'
          // }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码必须为6-16位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      eyeShow: true,
      passwordType: 'password'
    }
  },
  created() {
    // console.log('发送验证码')
    this.getverify()
  },
  methods: {
    async getverify() {
      const keys = randomNum()
      // console.log(keys)
      const res = await verify(keys)
      this.loginForm.key = keys
      // console.log(this.loginForm.key)
      console.log(res)
      // 将得到的二进制数据转换成Blob对象
      const blob = new Blob([res], { type: 'image/png' })
      console.log(blob)
      // 通过URL.createObjectURL动态生成一个可以渲染在img标签中的URL
      const imageUrl = window.URL.createObjectURL(blob)
      this.imageUrl = imageUrl
    },
    hLongin() {
      // 兜底校验
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          // console.log('发送登录请求')
          await this.$store.dispatch('user/login', this.loginForm)
          // 跳转首页
          this.$router.push('/')
        }
      })
    },
    eyeClick() {
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: #000;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  video {
    position: absolute;
  }
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  .login-form {
    width: 450px;
    height: 490px;
    padding: 36px 40px;
    max-width: 100%;
    margin: 0 auto;
    top: 45%;
    left: 45%;
    overflow: hidden;
    background-color: #fff;
    border-radius: 10px;
    z-index: 99;
    .button{
      width: 100%;
      height: 54px;
      margin-bottom: 14px;
      margin-top: 22px;
      background-color: #e15536;
      border: 0;
      font-size: 18px;
      color: #fff;
      font-weight: 700;
    }
    .code-image {
      display: inline-block;
      vertical-align: top;
      width: 71px;
      height: 32px;
      cursor: pointer;
      margin-left: 10px;
      position: absolute;
      right: 10px;
      top: 9px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    span {
      float: right;
    }
  }
  .title-container {
    position: relative;
    img {
      display: block;
      margin: 0 auto;
      margin-bottom: 20px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
